﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Microsoft.BizTalk.Samples.EDIGuidance.HelperComponents
{
    public class ItineraryHelper
    {
        public string GetItinerary(string itineraryName, string itineraryDBConnString)
        {
            return GetItinerary(itineraryName, -1, -1, itineraryDBConnString);
        }

        public string GetItinerary(string itineraryName, int majorVersion, int minorVersion, string itineraryDBConnString)
        {
            SqlConnection conn = new SqlConnection(itineraryDBConnString);

            SqlCommand command = new SqlCommand("Itinerary_getitinerary", conn);

            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add("@name", SqlDbType.NVarChar, 256).Value = itineraryName;
            if ((majorVersion == -1) && (minorVersion == -1))
            {
                command.Parameters.Add("@major", SqlDbType.BigInt).Value = null;
                command.Parameters.Add("@minor", SqlDbType.BigInt).Value = null;
            }
            else
            {
                command.Parameters.Add("@major", SqlDbType.BigInt).Value = majorVersion;
                command.Parameters.Add("@minor", SqlDbType.BigInt).Value = minorVersion;
            }


            conn.Open();

            SqlDataReader reader = command.ExecuteReader();

            reader.Read();

            string itineraryXML = reader["imITML"].ToString();

            conn.Close();

            return itineraryXML;
        }
    }
}
